package com.smart.spring_boot_empty.web;

import com.smart.spring_boot_empty.domain.entity.User;
import com.smart.spring_boot_empty.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value="/users")
public class UserController {

    @Autowired
    private IUserService userService;

//    static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

    @ApiOperation(value = "获取用户列表", notes = "")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<User> getUserList(@RequestParam(name ="pageNum", required = false, defaultValue = "1")
                                              int pageNum,
                                  @RequestParam(name ="pageSize", required = false, defaultValue = "1")
                                          int pageSize

                                  ){
        List<User> r = userService.getAllUsers(pageNum, pageSize); // new ArrayList<User>(users.values());
        return r;
    }
    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String postUser(@ModelAttribute User user){
//        users.put(user.getId(), user);
        userService.create(user);
        return "success";
    }
    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable int id){
        return userService.getUser(id);
    }

    @ApiOperation(value = "修改用户详细信息", notes = "根据url的id来修改用户数据，并将传过来User更新过去")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUser(@PathVariable Long id, @ModelAttribute User user){
//        User u = users.get(id);
//        u.setAge(user.getAge());
//        u.setName(user.getName());
//        users.put(id, u
// );
        userService.updateUser(user);
        return "success";
    }
    @ApiOperation(value = "删除用户信息", notes = "根据url的id来删除")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Integer id){
//        users.remove(id);
        userService.deleteById(id);
        return "success";
    }
}
